/*
 * Geometria.java
 *
 * Created on 17 de mayo de 2008, 07:32 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package futbol.forrest.gemetry;
import java.awt.Point;

/**
 *
 * @author Pablo
 */
public class Geometry {
    
    public static double EPSILON = 0.0001;  /*!< Value used for floating point equality tests. */    
            
    /** Creates a new instance of Geometria */
    public Geometry() {
    }
    
    /*! This function converts an angle in radians to the corresponding angle in
        degrees.
        \param x an angle in radians
        \return the corresponding angle in degrees */
    public static double Rad2Deg(double x){
      return (x * 180 / Math.PI);
    }

    /*! This function converts an angle in degrees to the corresponding angle in
        radians.
        \param x an angle in degrees
        \return the corresponding angle in radians */
    public static double Deg2Rad(double x){
      return ( x * Math.PI / 180 );
    }    
    
    public static double atan2Deg(double x, double y){
      if( Math.abs(x) < EPSILON && Math.abs( y ) < EPSILON )
        return 0.0;
      
      return Rad2Deg(Math.atan2(x, y));     
    }
    
    /*! This function returns the tangent of a given angle in degrees using the
        built-in tangent function that works with angles in radians.
        \param x an angle in degrees
        \return the tangent of the given angle */
    public static double tanDeg( double x ){
        return Math.tan( Deg2Rad( x ) );
    }

    /*! This function returns the cosine of a given angle in degrees using the
        built-in cosine function that works with angles in radians.
        \param x an angle in degrees
        \return the cosine of the given angle */
    public static double cosDeg( double x ){
      return ( Math.cos( Deg2Rad( x ) ) );
    }

    /*! This function returns the sine of a given angle in degrees using the
        built-in sine function that works with angles in radians.
        \param x an angle in degrees
        \return the sine of the given angle */
    public static double sinDeg( double x ){
      return ( Math.sin( Deg2Rad( x ) ) );
    }

    /*! This method performs the abc formula (Pythagoras' Theorem) on the given
        parameters and puts the result in *s1 en *s2. It returns the number of
        found coordinates.
        \param a a parameter in abc formula
        \param b b parameter in abc formula
        \param c c parameter in abc formula
        \param *s1 first result of abc formula
        \param *s2 second result of abc formula
        \return number of found x-coordinates */
    public static int abcFormula(double a, double b, double c, double s1, double s2){
      double dDiscr = b*b - 4*a*c;       // discriminant is b^2 - 4*a*c
      if (Math.abs(dDiscr) < EPSILON ){  // if discriminant = 0
        s1 = -b / (2 * a);               //  only one solution
        return 1;
      }else if (dDiscr < 0)              // if discriminant < 0
        return 0;                        //  no solutions
      else{                              // if discriminant > 0
        dDiscr = Math.sqrt(dDiscr);      //  two solutions
        s1 = (-b + dDiscr ) / (2 * a);
        s2 = (-b - dDiscr ) / (2 * a);
        return 2;
      }
    }    
    
    /*! This method returns the bisector (average) of two angles. It deals
        with the boundary problem, thus when 'angMin' equals 170 and 'angMax'
        equals -100, -145 is returned.
        \param angMin minimum angle [-180,180]
        \param angMax maximum angle [-180,180]
        \return average of angMin and angMax. */
    public static double getBisectorTwoAngles( double angMin, double angMax ){
      // separate sine and cosine part to circumvent boundary problem
      return VecPosition.normalizeAngle(
                atan2Deg( (sinDeg( angMin) + sinDeg( angMax ) )/2.0,
                          (cosDeg( angMin) + cosDeg( angMax ) )/2.0 ) );
    }
    
}
